package com.example.mvvm

import android.annotation.SuppressLint
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.JsonFactory
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.databind.module.SimpleModule
import java.io.IOException
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

object JacksonUtils {

    //时间序列化时变为时间戳
    internal class LocalDateTimeToLongSerializer :
        JsonSerializer<Date>() {
        @Throws(IOException::class, JsonProcessingException::class)
        override fun serialize(
            value: Date,
            jsonGenerator: JsonGenerator,
            serializers: SerializerProvider
        ) {
            jsonGenerator.writeNumber(value.time)
        }
    }

    //时间戳反序列化时间
    internal class LongToLocalDateTimeDeserializer :
        JsonDeserializer<Date>() {
        @Throws(IOException::class, JsonProcessingException::class)
        override fun deserialize(jsonParser: JsonParser, ctxt: DeserializationContext): Date {
            return Date(jsonParser.longValue)
        }
    }

    //时间序列化时变为字符串
    internal class LocalDateTimeToStringSerializer :
        JsonSerializer<Date?>() {
        @Throws(IOException::class, JsonProcessingException::class)
        override fun serialize(
            value: Date?,
            jsonGenerator: JsonGenerator,
            serializers: SerializerProvider
        ) {
            jsonGenerator.writeNumber('\"'.toString() + format.format(value) + '\"')
        }
    }

    //时间戳反序列化时间
    internal class StringToLocalDateTimeDeserializer :
        JsonDeserializer<Date?>() {
        @Throws(IOException::class, JsonProcessingException::class)
        override fun deserialize(jsonParser: JsonParser, ctxt: DeserializationContext): Date? {
            try {
                return format.parse(jsonParser.valueAsString)
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            return null
        }
    }

    val objectMapper: ObjectMapper

    @SuppressLint("SimpleDateFormat")
    val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US)

    init {
        val factory = JsonFactory()
        factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
        objectMapper = ObjectMapper()
        format.timeZone = TimeZone.getTimeZone("UTC")
        objectMapper.dateFormat = format
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
        val timeModule = SimpleModule()
//       timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeToLongSerializer());
//       timeModule.addDeserializer(LocalDateTime.class, new LongToLocalDateTimeDeserializer());
        timeModule.addSerializer(Date::class.java, LocalDateTimeToStringSerializer())
        timeModule.addDeserializer(Date::class.java, StringToLocalDateTimeDeserializer())
        objectMapper.registerModule(timeModule)
    }

}